/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.uc.service.impl;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

import com.xy.biz.uc.domain.bo.PositionBO;
import com.xy.biz.uc.domain.query.PositionQuery;
import com.xy.biz.uc.manager.PositionManager;
import com.xy.biz.uc.service.PositionService;
import com.xy.tool.context.UserContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xy.lang.domain.Result;

/**
 *
 *
 * @author zgy
 * @date 2021-9-23
 */
@Service
public class PositionServiceImpl implements PositionService {

    @Autowired
    private PositionManager positionManager;

    @Override
    public Result<?> savePosition(PositionBO positionBO) {
        build(positionBO);
        int affectedRows = positionManager.savePosition(positionBO);
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    private void build(PositionBO positionBO) {
        LocalDateTime now = LocalDateTime.now();
        positionBO.setTenantId(UserContextHolder.getTenantId());
        positionBO.setCreateTime(now);
        positionBO.setUpdateTime(now);
        positionBO.setDeleteFlag(false);
    }

    @Override
    public Result<?> saveAllPosition(Collection<PositionBO> collection) {
        int affectedRows = positionManager.saveAllPosition(collection);
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    @Override
    public Result<?> updatePosition(PositionBO positionBO) {
        int affectedRows = positionManager.updatePosition(positionBO);
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    @Override
    public Result<?> deletePosition(Long positionId) {
        int affectedRows = positionManager.deletePosition(positionId);
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    @Override
    public PositionBO getPosition(Long positionId) {
        return positionManager.getPosition(positionId);
    }

    /**
     * 根据UserId查找一个
     *
     * @param userId
     * @return
     */
    @Override
    public PositionBO getPositionByUserId(Long userId) {
        return positionManager.getPositionByUserId(userId);
    }

    @Override
    public List<PositionBO> listPositionById(Collection<Long> collection) {
        return positionManager.listPositionById(collection);
    }

    @Override
    public List<PositionBO> listPositionByUserId(Collection<Long> collection) {
        return positionManager.listPositionByUserId(collection);
    }

    @Override
    public List<PositionBO> listPosition(PositionQuery query) {
        return positionManager.listPosition(query);
    }

    @Override
    public int countPosition(PositionQuery query) {
        return positionManager.countPosition(query);
    }

}
